package Servlet;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */


import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author ad
 */
public class index_jsp extends HttpServlet {
   
    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
            //por si entra sin loguearse al serlet
        HttpSession sesionOk = request.getSession();
            if (sesionOk.getAttribute("Usuario") == null) {
            response.setContentType("text/html;charset=UTF-8");
            response.sendRedirect("/ProyectoBases2/index.jsp");
        }
    } 

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        System.out.println("entrea");
        processRequest(request, response);
    } 

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {

response.setHeader("Pragma","no-cache");
response.setHeader("Cache-Control","no-store");
response.setHeader("Expires", "0");
response.setHeader("Cache", "no-cache");
response.setDateHeader("Expires",-1);

String usuario = "";
String clave = "";

if (request.getParameter("Usuario") != null)
    {
usuario = request.getParameter("Usuario");
}
if (request.getParameter("Clave") != null)
clave = request.getParameter("Clave");


if (usuario.equals("diego") && clave.equals("123"))
{
      PrintWriter out = response.getWriter();
        try {
             
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet index_jsp</title>");
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>Servlet index_jsp at " + request.getContextPath () + "</h1>");
            out.println("</body>");
            out.println("</html>");
    }catch(Exception x)
        {

    }

}
/*HttpSession sesionOk = request.getSession();
sesionOk.setAttribute("Usuario",usuario);
%>
<jsp:forward page="menu.jsp" />
<%
}

else {
%>
<jsp:forward page="index.jsp">
<jsp:param name="error" value="Usuario y/o clave
incorrectos.<br>Vuelve a intentarlo."/>
</jsp:forward>
<%
}
%>
  */

        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
